﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameName1
{
    abstract class GameObject
    {
        protected Vector2 m_Pos;
        protected float m_width;
        protected float m_height;

        protected float m_health;
        protected float m_armor;

        protected Rectangle m_bbox;
        protected double m_atkcd;
        protected const double DEFAULT_ATKCD = 0.2f;
        protected TextureHandler m_texhandler;
        /// <summary>
        /// TRUE IS RIGHT
        /// FALSE IS LEFT
        /// </summary>
        protected bool m_facing; //TRUE IS RIGHT FALSE IS LEFT
        protected bool m_moved;

        protected Vector2 m_offsetL;
        protected Vector2 m_offsetR;

        protected GameObject()
        {
            m_Pos = new Vector2(0, 0);
            m_width = 16.0f;
            m_height = 16.0f;
            m_health = 1.0f;
            m_armor = 0.0f;
            m_facing = true;
            m_texhandler = new TextureHandler();
            m_bbox = new Rectangle((int)m_Pos.X, (int)m_Pos.Y, (int)(m_Pos.X + m_width), (int)(m_Pos.Y + m_height));
        }

        protected GameObject(Vector2 i_Pos)
        {
            m_Pos = i_Pos;
            m_bbox = new Rectangle((int)m_Pos.X, (int)m_Pos.Y, (int)(m_Pos.X + m_width), (int)(m_Pos.Y + m_height));

            m_texhandler = new TextureHandler();
            m_moved = false;
        }

        /// <summary>
        /// TRUE IS RIGHT
        /// FALSE IS LEFT
        /// </summary>
        public bool Facing
        {
            get {return m_facing;}
        }

        public TextureHandler TexHandler
        {
            get { return m_texhandler; }
        }

        public bool HasMoved
        {
            get { return m_moved; }
            set { m_moved = value; }
        }

        public void Move(float d_X)
        {
            m_Pos += new Vector2(d_X, 0);
            UpdateBBox();
            UpdateFacing(d_X);

        }

        public void Move(float d_X, float d_Y)
        {
            m_Pos += new Vector2(d_X, d_Y);
            UpdateBBox();
            UpdateFacing(d_X);

        }

        public void Move(Vector2 delta)
        {
            m_Pos += delta;
            UpdateBBox();
            UpdateFacing(delta.X);
        }

        public virtual void Update(double t)
        {
            if (m_atkcd > 0.0f)
            {
                m_atkcd -= t;
            }

            m_texhandler.Update(t);
        }

        private void UpdateBBox()
        {
            //TO DO: MAKE IT UPDATE ON BBOX SIZE...
            //m_bbox = new Rectangle((int)m_Pos.X, (int)m_Pos.Y, (int)(m_Pos.X + m_width), (int)(m_Pos.Y +  m_height));
            //TO DO: Add offsets for bounding boxes
            m_bbox = new Rectangle((int)m_Pos.X + 16, (int)m_Pos.Y + 14, (int)m_width, (int)m_height);
        }

        private void UpdateFacing(float dx)
        {
            if (dx < 0)
            {
                m_facing = false;
                m_texhandler.MoveState = TextureHandler.MOVE_STATE.MOVINGL;
            }
            else if(dx > 0)
            {
                m_facing = true;
                m_texhandler.MoveState = TextureHandler.MOVE_STATE.MOVINGR;
            }
        }

        public void Shoot(Projectile.PROJ_TYPE type)
        {
            if (m_atkcd > 0)
                return;

            if (m_facing)
            {
                GameObject t = (GameObject)this;
                Projectile.proj.Add(new Projectile(m_Pos+m_offsetR, new Vector2(1, 0), type, ref t));
                m_atkcd = DEFAULT_ATKCD;
            }
            else
            {
                GameObject t = (GameObject)this;
                Projectile.proj.Add(new Projectile(m_Pos+m_offsetL, new Vector2(-1, 0), type, ref t));

                m_atkcd = DEFAULT_ATKCD;
            }
        }

        public abstract void Draw(ref SpriteBatch sbatch);
        protected virtual void DrawCBox(ref SpriteBatch sbatch) { }

        protected void DrawColBox(ref SpriteBatch sbatch)
        {
            Texture2D cbox = new Texture2D(sbatch.GraphicsDevice, 1, 1);
            cbox.SetData<Color>(new Color[] { Color.White });
            int lineWidth = 1;

            sbatch.Draw(cbox, new Rectangle(m_bbox.X, m_bbox.Y, lineWidth, m_bbox.Height), Color.Red);
            sbatch.Draw(cbox, new Rectangle(m_bbox.X, m_bbox.Y, m_bbox.Width + lineWidth, lineWidth), Color.Red);
            sbatch.Draw(cbox, new Rectangle(m_bbox.X + m_bbox.Width, m_bbox.Y, lineWidth, (m_bbox.Height + lineWidth)), Color.Red);
            sbatch.Draw(cbox, new Rectangle(m_bbox.X, m_bbox.Y + m_bbox.Height, m_bbox.Width + lineWidth, lineWidth), Color.Red);
        }
        


    }
}
